﻿/**
 * ...
 * @author kex
 */

package ;

import flash.display.Sprite;
import flash.ui.Keyboard;
import flash.display.Shape;
import flash.events.Event;
import flash.utils.Timer;
import gui.hug.HUGButton;
import gui.hug.HUGColors;
import gui.hug.HUGCheckBox;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.events.KeyboardEvent;
import gui.hug.HUGEditField;
import gui.hug.HUGListBox;
import gui.hug.HUGMultiLabel;
import gui.hug.LightButton;

class MyButton {
	
	/*** AMOUNT OF CIRCLES FOR EACH EVENT ***/
	public static var CLICK : Int;
	public static var OVER : Int;
	public static var OUT : Int;
	public static var DOWN : Int;
	public static var UP : Int;
	public static var MOVE : Int;
	public static var WHEEL : Int = 1;
	

	private var window : Sprite;
	
	private static inline var defaultButtonLabel : String = "DRAW";
	private var currentButtonLabel : String;
	
	/*** CONTENT ***/
	private var button 		: LightButton;
	private var checkbox 	: HUGCheckBox;
	private var field		: HUGEditField;
	private var timer 		: Timer;
	private var list 		: MyList;
	
	
	public function new(instance : Sprite) {
        window = instance;
		init();
		addButtonEvents();
	}
	
	/*** WATCHING EVENTS***/
	private function addButtonEvents() {
		//button.addEventListener(MouseEvent.CLICK, buttonClicked);
		button.addEventListener(MouseEvent.MOUSE_OVER, mouseOver); 
		button.addEventListener(MouseEvent.MOUSE_OUT, mouseOut);
		button.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);	
		button.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
		//button.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
		button.addEventListener(MouseEvent.MOUSE_WHEEL, mouseWheel);
		timer = new Timer(1700, 1);
	}
	
	/*** MAKING CONTENT AND DESIGN***/
	private function init() {
		currentButtonLabel = defaultButtonLabel;
		CLICK = 10;
		OVER = 2;
		OUT = 2;
		DOWN = 2;
		UP = 2;
		MOVE = 1;
		WHEEL = 1;
				
		button = new LightButton(defaultButtonLabel, 480, 20, 120, HUGColors.VK_BORDER_INNER, HUGColors.VK_BUTTON, HUGColors.VK_BUTTON, HUGColors.WHITE, 0, 16);
		window.addChild(button);
		
		checkbox = new HUGCheckBox("set button text:", 480, 55);
		checkbox.addEventListener(MouseEvent.MOUSE_DOWN, checkBoxSelected);
		window.addChild(checkbox);
		
		field = new HUGEditField();
		field.setLocation(480, 70);
		field.setSize(120, 17);
		window.addChild(field);
	}
	
	/*** CIRCLES ***/	
	public function drawCircles(quantity : UInt) {
		var circle : Shape = new Shape();
		for(i in 0...quantity) {
			circle.graphics.beginFill( Std.int(Math.random() * 2000000000) , Math.random() + 0.3 );
			circle.graphics.drawCircle( Std.int(Math.random() * 400) , Std.int(Math.random() * 500) , Std.int(Math.random() * 59) + 10 );
			window.addChild( circle );
		}
	}
	
	
	/*** CHECKBOX EVENT FUNCTION ***/
	private function checkBoxSelected(_) {
		if (checkbox.getChecked()) {
			var len : Int = ( field.getText().length*12 + 40 > 120 ) ? field.getText().length*12 + 40 : 120;
			currentButtonLabel = field.getText();
			button.setLabel( currentButtonLabel );
			button.width = len;
		} else {
			button.width = 120;
			currentButtonLabel = defaultButtonLabel;
			button.setLabel( defaultButtonLabel );
		}
	}

	/*** BUTTON EVENT FUNCTIONS***/
	private function buttonClicked(_):Void {
		button.setLabel("CLICK");
		drawCircles(CLICK);
		timer.reset();
		timer.start();
		timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerComplete);
	}
	private function mouseOver(_):Void {
		button.setLabel("MOUSE OVER");
		drawCircles(OVER);
		timer.reset();
		timer.start();
		timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerComplete);
	}
	private function mouseUp(_):Void {
		button.setLabel("MOUSE UP");
		drawCircles(UP);
		timer.stop(); 
		timer = new Timer(1700, 1);
		timer.start();
		timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerComplete);
	}
	private function mouseDown(_):Void {
		button.setLabel("MOUSE DOWN");
		drawCircles(DOWN);
		timer.reset(); 
		timer.start();
		timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerComplete);
	}
	private function mouseMove(_):Void {
		button.setLabel("MOUSE MOVE");
		drawCircles(MOVE);
		timer.stop(); 
		timer = new Timer(1700, 1);
		timer.start();
		timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerComplete);
	}
	private function mouseOut(_):Void {
		button.setLabel("MOUSE OUT");
		drawCircles(OUT);
		timer.reset();
		timer.start();
		timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerComplete);
	}
	private function mouseWheel(_):Void {
		button.setLabel("MOUSE WHEEL");
		drawCircles(WHEEL);
		timer.reset();
		timer.start();
		timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerComplete);
	}
	private function timerComplete(_):Void {
		if (checkbox.getChecked())
			button.setLabel(field.text);
		else
			button.setLabel(defaultButtonLabel);
	}
}